package com.hinner.common;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private JwtInterceptor jwtInterceptor;


    /**
     * 访问项目资源时，给他一个访问前缀 /hinner
     * @param configurer
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.addPathPrefix("/hinner",clazz -> clazz.isAnnotationPresent(RestController.class));
    }



    /**
     * 添加自定义拦截器，设置拦截规则,对所有的/hinner开头的url都要做拦截验证；
     * 对想要不验证的url可以直接进行放行。
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //排除登录、注册接口，不需要jwt的token验证，其他所有/hinner开头的接口都要token验证
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/hinner/**")
                .excludePathPatterns("/hinner/**") //开发测试的时候不要做token的验证
                .excludePathPatterns("/hinner/user/login")
                .excludePathPatterns("/hinner/user/wx/login")
                .excludePathPatterns("/hinner/user/register")
                .excludePathPatterns("/hinner/user/forgot-password")
                .excludePathPatterns("/hinner/test/**")
                .excludePathPatterns("/hinner/wx/user/sessionKey");
    }

}
